package FirstHomeworks; /**
 * Created by Yulia on 13.03.14.
 */
import junit.framework.Assert;
import org.testng.annotations.*;
import org.testng.annotations.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.regex.*;

public class FileReader
{
    @Parameters({ "String1", "String2", "String3", "Path" })

    @BeforeTest
    public void setUp(String String1, String String2, String String3, String Path)
    {
        {
            try {

                File file = new File(Path);

                if (file.createNewFile()){
                    System.out.println("File is created!");
                }else{
                    System.out.println("File already exists.");
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(String1);
                bw.newLine();
                bw.write(String2);
                bw.newLine();
                bw.write(String3);
                bw.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Parameters("Path")
    @Test
    public void readFile(String Path) {
        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new java.io.FileReader(Path));
            ArrayList <Integer> numbers = new ArrayList();

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                Pattern pattern = Pattern.compile("[-]?[0-9]+(.[0-9]+)?");
                Matcher matcher = pattern.matcher(sCurrentLine);


                while (matcher.find()) {
                    System.out.println(matcher.group());
                    String s = matcher.group();
                    int number=Integer.parseInt(s);
                    numbers.add(number);

                }
            }
            System.out.println(numbers);

            for (int j = 0; j < numbers.size() - 1; j++)
            {
                if(numbers.get(j) <= numbers.get(j + 1)){
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }
                Assert.assertTrue(numbers.get(j) <= numbers.get(j + 1));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

    @Parameters("Path")
    @AfterTest
    public void tearDown(String Path){
        try{

            File file = new File(Path);

            if(file.delete()){
                System.out.println(file.getName() + " is deleted!");
            }else{
                System.out.println("Delete operation is failed.");
            }

        }catch(Exception e){

            e.printStackTrace();

        }

    }

}
